<?php

namespace App\Admin\Controllers;

use App\Models\TaskAudit;
use App\Models\MoneyLog;
use App\Models\Wallet;
use App\Models\MoneyLogType;
use App\Models\Task;
use App\Models\User;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\HasResourceActions;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;
use Encore\Admin\Show;
use Encore\Admin\Layout\Column;
use Encore\Admin\Layout\Row;
use Encore\Admin\Widgets\Box;
use Encore\Admin\Widgets\InfoBox;
use Encore\Admin\Widgets\Table;
use Illuminate\Http\Request;
use App\Admin\Extensions\Pay;
use DB;

class TaskPayController extends Controller
{
    use HasResourceActions;

    /**
     * Index interface.
     *
     * @param Content $content
     * @return Content
     */
    public function index(Content $content)
    {
        return $content
            ->header('任务结算')
            ->description('任务结算')
            ->row(function (Row $row) {
                $row->column(3, function (Column $column) {
                    $total = TaskAudit::count();
                    $infoBox = new InfoBox('提交任务总数', 'tasks', 'aqua', '/admin/taskaudit', $total);
                    $column->append($infoBox);
                });
                $row->column(3, function (Column $column) {
                    $today_total = TaskAudit::whereDate('created_at', date('Y-m-d'))->count();
                    $infoBox = new InfoBox('今日新提交任务', 'tasks', 'aqua', '', $today_total);
                    $column->append($infoBox);
                });

            })
            ->body($this->grid());
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new TaskAudit);
        $grid->model()->wherein('status',[1,2]);
        $grid->id('Id');
        $grid->task_id('任务名称')->display(function($task_id){
            $data = Task::where('id',$task_id)->first();
            if ($data) {
                return $data->name;
            }
        });
        $grid->user_id('用户昵称')->display(function($user_id){
            $data = User::where('id',$user_id)->first();
            if ($data) {
                return $data->name;
            }
        });
        $grid->column('手机号')->display(function($user_id){
            $data = User::where('id',$this->user_id)->first();
            if ($data) {
                return $data->phone;
            }
        });

        $grid->status('任务状态')->display(function($status){
            switch ($status) {
                case '0':
                    return "<span class='label label-warning'>未审核</span>";
                    break;
                case '1':
                    return "<span class='label label-primary'>通过未打款</span>";
                    break;
                case '2':
                    return "<span class='label label-success'>通过已打款</span>";
                    break;
                case '3':
                    return "<span class='label label-danger'>未通过</span>";
                    break;
                default:
                    # code...
                    break;
            }
        });
        $grid->created_at('创建时间');
        $grid->updated_at('更新时间');
        $grid->disableCreateButton();
        $grid->disableRowSelector();
        
        $grid->actions(function ($actions) {
            $actions->disableDelete();
            $actions->disableEdit();
            $actions->disableView();            
            $status = $actions->row;           
            $actions->append("<a href='/admin/taskdetail/" . $actions->getKey() . "'>" . '<button type="button" class="btn btn-primary">任务详情</button>' . "</a>");
            $row = $actions->row;
            if($row['status'] == 1){
                $actions->append(new Pay($actions->getKey()));                                                       
            }
        });
        
        $grid->filter(function ($filter) {
            // 去掉默认的id过滤器
            $filter->disableIdFilter();
            $filter->equal('status', '任务状态')->radio([
                0 => '未审核',
                1 => '通过未打款',
                2 => '通过已打款',
            ]);
            $filter->scope('未审核')->where('status', 0);
            $filter->scope('今日新增', '今日新增')->whereDate('created_at', date('Y-m-d'));

        });

        return $grid;
    }
    
    //审核打款
    public function taskpay(Request $request)
    {       
        $id = $request->id;
        $audit = TaskAudit::where('id',$id)->with('task')->first();
        $moneylogtype = MoneyLogType::where('id',1)->first();
        $wallet = Wallet::where('user_id',$audit->user_id)->first();
        DB::beginTransaction();
        try {
            $audit->status = 2;
            $audit->reason = $request->reason;
            $audit->save();
            $wallet->money = $wallet->money+$audit->task->money;
            $wallet->sum_money = $wallet->sum_money+$audit->task->money;
            $wallet->save();
            $moneylog = new MoneyLog;
            $moneylog->type_id = 1;
            $moneylog->user_id = $audit->user_id;
            $moneylog->type_name = $moneylogtype->name;
            $moneylog->status = 1;
            $moneylog->before = $wallet->money;
            $moneylog->after = $wallet->money+$audit->task->money;
            $moneylog->change_money = $audit->task->money;
            $moneylog->save();
            DB::commit();
            return $this->ajax_success('操作成功');
        } catch (QueryException $ex) {
            DB::rollback();
            return $this->ajax_error('操作失败');
        }
        
    }


}
